<?php
require_once ('Controller/Admin/Common.inc.php');
require_once ('View/Result.inc.php');
require_once ('Model/CircleModel.inc.php');
require_once ('Model/DeviceModel.inc.php');
require_once ('Model/DeviceToCircleModel.inc.php');
require_once ('Model/MessageModel.inc.php');
final class Controller extends common {
//	private $Loader;
	private $CircleModel;
	private $DeviceModel;
	private $DeviceToCircleModel;
	private $diaplayrows = 20;
	public function execude () {
		$this->CircleModel = new CircleModel ($this->mysql);
		$this->DeviceModel = new DeviceModel ($this->mysql);
		$this->DeviceToCircleModel = new DeviceToCircleModel ($this->mysql);
		$this->MessageModel = new MessageModel ($this->mysql);
		$trace = debug_backtrace ();
		if (isset ($trace[1])) {
			$this->Loader = $trace[1]['object'];
		}
		if ($this->checklogin ()) {
			if (array_key_exists ('action', $_REQUEST)) {
				switch ($_REQUEST['action']) {
					case 'addmessage':
					default:
						$this->sendPage ();
						break;
				}
			} else {
				$this->sendPage ();
			}
		}
	}

	private function addMappingPage () {
		if (array_key_exists ('id', $_REQUEST) && ctype_digit ($_REQUEST['id']) && ($DeviceInfo = $this->DeviceModel->CheckDeviceItemExistByID ($_REQUEST['id'])) != NULL) {
			if ($_SERVER['REQUEST_METHOD'] == 'POST') {
				if (ctype_digit ($_REQUEST['Circle_ID']) && ($CircleInfo = $this->CircleModel->CheckCircleItemExist ($_REQUEST['Circle_ID'])) != NULL) {
					$DeviceToCircleInfo = $this->DeviceToCircleModel->GetCirclesByDevice ($_REQUEST['id']);
					if (count ($DeviceToCircleInfo) > 0) {
						if ($DeviceToCircleInfo[0][$this->DeviceToCircleModel->circleIDRowName] == $_REQUEST['Circle_ID']) {
							$this->template->addWarningMesage ('Edit Successful');
							$this->DeviceToCircleModel->UpdateDeviceToCircle ($DeviceToCircleInfo[0][$this->DeviceToCircleModel->idRowName], $_REQUEST['id'], $_REQUEST['Circle_ID'], $_REQUEST['StartDateTime'], $_REQUEST['EndDateTime']);
							$this->listPage ();
						} else {
							$this->template->addWarningMesage ('Device was maped');
							$this->NaddMappingPage ($DeviceInfo, $_REQUEST['id'], $_REQUEST['Circle_ID'], $_REQUEST['StartDateTime'], $_REQUEST['EndDateTime']);
						}
					} else {
						$this->template->addWarningMesage ('Add Successful');
						$this->DeviceToCircleModel->AddDeviceToCircle ($_REQUEST['id'], $_REQUEST['Circle_ID'], $_REQUEST['StartDateTime'], $_REQUEST['EndDateTime']);
						$this->listPage ();
					}
				}
			} else {
				$this->NaddMappingPage ($DeviceInfo, $_REQUEST['id']);
			}
		} else {
			$this->template->addWarningMesage ('no record here');
			$this->listPage ();
		}
	}

////////////////////////////
	private function NsendPage ($Circle_ID = NULL, $text = NULL) {
		$messagesendtemplate = new template ();
		$messagesendtemplate->load ('html/MessageSendManagerSend.html');
		$messagesend = $messagesendtemplate->dom->documentElement;
		$messagesend = $this->template->dom->importNode ($messagesend, true);
		$messagesend->setAttribute ('action', sprintf ('?module=%s&action=addmessage', $this->Loader->module));
		$node = $this->template->query ('table/tr/td/select[@name="Circle_ID"]/option', $messagesend)->item (0);
		foreach ($this->CircleModel->GetCircleItems (0, $this->CircleModel->GetCircleCount ()) as $item) {
			if ($item[$this->CircleModel->typeRowName] == 1) {
				$newnode = $node->cloneNode (true);
				if ($Circle_ID != NULL && $item[$this->CircleModel->idRowName] == $Circle_ID) {
					$newnode->setAttribute ('selected', 'selected');
				}
				$newnode->setAttribute ('value', $item[$this->CircleModel->idRowName]);
				$newnode->nodeValue = htmlspecialchars ($item[$this->CircleModel->nameRowName]);
				$node->parentNode->appendChild ($newnode);
			}
		}
		if ($text != NULL) {
			$node = $this->template->query ('table/tr/td/textarea[@name="text"]', $messagesend)->item (0);
			$node->nodeValue = htmlspecialchars ($text);
		}
		$this->template->content->appendChild ($messagesend);
	}

	private function sendPage () {
		$CircleID = NULL;
		$MessageText = NULL;
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$OK = true;
			if (!array_key_exists ('Circle_ID', $_REQUEST) || !ctype_digit ($_REQUEST['Circle_ID'])) {
				$this->template->addWarningMesage ('Please Select One Circle');
				$OK = false;
			} else if (($CircleInfo = $this->CircleModel->CheckCircleItemExist ($_REQUEST['Circle_ID'])) == NULL || $CircleInfo[$this->CircleModel->typeRowName] != 1 ||$this->DeviceToCircleModel->CheckCircleAvailable ($_REQUEST['Circle_ID']) < 1) {
				$this->template->addWarningMesage ('Please Select One Valid Circle');
				$OK = false;
			} else {
				$CircleID = $_REQUEST['Circle_ID'];
			}
			if (!array_key_exists ('text', $_REQUEST) || strlen ($_REQUEST['text']) <= 1) {
				$this->template->addWarningMesage ('Please Input a message');
				$OK = false;
			} else {
				$MessageText = $_REQUEST['text'];
			}
			if ($OK) {
				$this->template->addWarningMesage ('Send Successful');
				$this->MessageModel->AddMessageItem ($_REQUEST['text'], 0, $_REQUEST['Circle_ID']);
				$CircleID = NULL;
				$MessageText = NULL;
			}
		}
		$this->NsendPage ($CircleID, $MessageText);
/*
			if ($CircleInfo[$this->CircleModel->typeRowName] == 1) {
			} else {
				$this->template->addWarningMesage ('Please Select a Room Circle');
			}
			// && ($CircleInfo = $this->CircleModel->CheckCircleItemExist ($_REQUEST['Circle_ID'])) != NULL

*/
	}
}
?>
